<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Log;
use think\Request;

class Task extends Controller
{
    public function __construct()
    {
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
  
    }

    protected $table_DB_Platform_Account_library = 'db_config2';               /*平台账户配置库*/
    protected $table_DB_Platform_Account_operatelog_library = 'db_operatelog';  /*数据统计库*/
    protected $accounts_info = 'accounts_info';                                          /* 用户表 */
    protected $game_burying_point_user = 'game_burying_point_user';
    protected $game_Burying_point = 'game_burying_point';                                /* 打点日志表 *//* 打点用户表 */
    protected function Db_connect_game_Burying_point()
    {
        $game_Burying_point = Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_Burying_point);
        return $game_Burying_point;
    }


    protected function Db_connect_game_burying_point_user()
    {
        $game_burying_point_user = Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_burying_point_user);
        return $game_burying_point_user;
    }

    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect($this->table_DB_Platform_Account_library)->table($this->accounts_info);
        return $accounts_info;
    }

    public function user_Burying_point(Request $request)
    {
        set_cors();
        $param = $request->param();

        if($param['UserID'] == 1){
            $this->userOne($param);
            putsuccess(1);
        }

        if (empty($param['point_ID']) || empty($param['UserID']) || $param['UserID'] == 1) {
            puterror(1);
        }
        $device = isset($param['Device'])?$param['Device']:0;
        $res = $this->burying_insert($param['point_ID'], $param['SpreaderID'], $param['UserID'],$device);
        if ($res == 'success') {
            putsuccess(1);
        }
    }
    //用户id为1的时候走独立业务逻辑
    public  function userOne($param){
        $map['SpreaderID'] = $param['SpreaderID'];
        $map['point_ID'] = $param['point_ID'];
        $map['date'] = date('Y-m-d', time());

        $result = $this->Db_connect_game_Burying_point()->field('ID')->where($map)->find();
        if($result){
            $this->Db_connect_game_Burying_point()->where($map)->setInc('count', 1);
        }else{
            $this->Db_connect_game_Burying_point()->insert($map);
        }

    }

    /*判断是否是用户类型（新增活跃）1、新增用户 2、活跃用户 3、其他类型*/
    function user_type($UserID)
    {
        $begin_time = date('Y-m-d H:i:s', strtotime(date('Y-m-d', time())));
        $after_time = date('Y-m-d H:i:s', strtotime(date('Y-m-d', time() + 24 * 3600)));

        if (empty($UserID)){
            return false;
        }

        $where = [
            'UserID' => $UserID
        ];

        $res = $this->Db_connect_accounts_info()->where($where)
            ->whereTime('RegisterDate', 'between', [$begin_time, $after_time])
            ->find();

        if ($res) {
            return 1;
        } else {
            $result = $this->Db_connect_accounts_info()
                ->whereTime('LastLoginDate', 'between', [$begin_time, $after_time])
                ->find();
            if ($result) {
                return 2;
            } else {
                return 3;
            }
        }
    }

    /*判断是否是用户类型（新增活跃）1、新增用户 2、活跃用户 3、其他类型*/
    function user_type_device($Device)
    {
        $begin_time = date('Y-m-d H:i:s', strtotime(date('Y-m-d', time())));
        $after_time = date('Y-m-d H:i:s', strtotime(date('Y-m-d', time() + 24 * 3600)));

        // 根据uid位数来判断是否需要查询设备号
        if (empty($Device)) {
            return false;

        }

        $where = [
            'RegisterMachine' => $Device
        ];

        $res = $this->Db_connect_accounts_info()->where($where)
            ->whereTime('RegisterDate', 'between', [$begin_time, $after_time])
            ->find();

        if ($res) {
            return 1;
        } else {
            $result = $this->Db_connect_accounts_info()
                ->whereTime('LastLoginDate', 'between', [$begin_time, $after_time])
                ->find();
            if ($result) {
                return 2;
            } else {
                return 3;
            }
        }
    }

    function burying_insert($point_ID, $SpreaderID, $UserID,$device)
    {
        $map = [];
        $map['SpreaderID'] = $SpreaderID;
        $map['point_ID'] = $point_ID;
        $map['date'] = date('Y-m-d', time());

        $user_type = $this->user_type($UserID);
        $user_type2 = $this->user_type_device($device);

        /*查询打点记录是否存在*/
        $result = $this->Db_connect_game_Burying_point()->field('ID')->where($map)->find();
        if ($result) {
            $re = $this->Db_connect_game_burying_point_user()
                ->where('UserID', $UserID)
                ->where('P_ID', $result['ID'])
                ->find();
            if (empty($re)) {
                $data['UserID'] = $UserID;
                $data['P_ID'] = $result['ID'];

                // 人数
                $this->Db_connect_game_Burying_point()->where($map)->setInc('Number', 1);
                $data['type_ID'] = $user_type;

                // 新增人数
                if ($user_type == 1) {
                    $this->Db_connect_game_Burying_point()->where($map)->setInc('add_Number', 1);
                }

                // 新增设备数
                if ($user_type2 == 1) {
                    // 判断设备号 添加字段 新增设备人数 设备次数 仅更新这两个字段
                    // Db_connect_game_burying_point_user 这里的uid存设备id
                    $this->Db_connect_game_Burying_point()->where($map)->setInc('add_device_number', 1);
                    mylog('debug2 user_type2 = 1');
                }

                try {
                    $this->Db_connect_game_burying_point_user()->insert($data);
                }catch (\Exception $e){
                    mylog($e->getMessage());
                }

            } else {
                $this->Db_connect_game_burying_point_user()
                    ->where('UserID', $UserID)
                    ->where('P_ID', $result['ID'])
                    ->setInc('count', 1);
            }

        } else {
            $result = $this->Db_connect_game_Burying_point()->insert($map);
            if ($result) {
                $ID = $this->Db_connect_game_Burying_point()->getLastInsID();
                $re = $this->Db_connect_game_burying_point_user()
                    ->where('UserID', $UserID)
                    ->where('P_ID', $result['ID'])
                    ->find();
                if (empty($re)) {
                    $data['UserID'] = $UserID;
                    $data['P_ID'] = $ID;
                    $data['type_ID'] = $user_type;

                    // 新增人数
                    if ($user_type == 1) {
                        $this->Db_connect_game_Burying_point()->where($map)->setInc('add_Number', 1);
                    }

                    // 新增设备数
                    if ($user_type2 == 1) {
                        // 判断设备号 添加字段 新增设备人数 设备次数 仅更新这两个字段
                        // Db_connect_game_burying_point_user 这里的uid存设备id
                        $this->Db_connect_game_Burying_point()->where($map)->setInc('add_device_number', 1);
                        mylog('debug2 user_type2 = 1');
                    }

                    if(in_array($SpreaderID,[584,585,586,587,588])){
                        if($UserID > 11130665){// 2020-09-28以后开始注册
                            $this->Db_connect_game_Burying_point()->where($map)->setInc('Number', 1);
                        }
                    }else{
                        $this->Db_connect_game_Burying_point()->where($map)->setInc('Number', 1);
                    }

                    try {
                        $this->Db_connect_game_burying_point_user()->insert($data);
                    }catch (\Exception $e){
                        mylog($e->getMessage().' second');
                    }
                } else {
                    $this->Db_connect_game_burying_point_user()
                        ->where('UserID', $UserID)
                        ->where('P_ID', $result['ID'])
                        ->setInc('count', 1);
                }
            }
        }
        switch ($user_type) {
            case 1:
                $this->Db_connect_game_Burying_point()->where($map)->setInc('active_count', 1);
                $this->Db_connect_game_Burying_point()->where($map)->setInc('new_count', 1);
                break;
            case 2:
                $this->Db_connect_game_Burying_point()->where($map)->setInc('active_count', 1);
                break;
        }


        switch ($user_type2) {
            case 1:
                // 考虑设备号的情况
                $this->Db_connect_game_Burying_point()->where($map)->setInc('add_device_count', 1);
                mylog('debug user_type2 = 1');
                break;
        }

        $this->Db_connect_game_Burying_point()->where($map)->setInc('count', 1);
        return 'success';
    }

    // 记录用户状态
    public function user_status(Request $request)
    {
        $param = $request->param();
        if (empty($param['content']) || empty($param['UserID'])) {
            puterror(1);
        }
        $param['create_time'] = date('Y-m-d H:i:s');
        $count = db('game_burying_user','db_operatelog')->insert($param);
        putsuccess($count);
    }


    public function device(Request $request){
        $param = $request->param();
        $count = db('game_device','db_operatelog')->insert([
            'device'=>$param['device'],
            'channel_id'=>$param['channel_id'],
            'point_id'=>$param['point_id'],
            'param'=>empty($param['param'])?'':$param['param'],
            'date'=>date('Y-m-d H:i:s'),
        ]);
        exit('success');
    }
}